package com.jinlong.service.impl;

import com.jinlong.api.server.UserService;
import com.jinlong.common.api.BaseApiService;
import com.jinlong.entity.UserEntity;
import com.jinlong.manage.UserServiceManage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
@RestController
@Slf4j
public class UserServiceImpl extends BaseApiService implements UserService {
    @Autowired
    private UserServiceManage userServiceManage;
    @Override
    public Map<String, Object> regist(@RequestBody UserEntity userEntity) {
        if(StringUtils.isEmpty(userEntity.getUser_name())){
            return setResultError("用户名称不能为空");
        }
        if(StringUtils.isEmpty(userEntity.getPass_word())){
            return setResultError("密码不能为空");
        }
        try{
            userServiceManage.regist(userEntity);
            return setResultSuccess();
        }catch (Exception e){
            log.error("### regist() ERROR:",e);
            return setResultError("注册失败");
        }
    }

    @Override
    public Map<String, Object> login(@RequestBody UserEntity userEntity) {
        return userServiceManage.login(userEntity);
    }

    @Override
    public Map<String, Object> getUser(@RequestParam("token") String token) {
        if(StringUtils.isEmpty(token)){
            return setResultParameterError("token不能为空");
        }
        return userServiceManage.getUser(token);
    }

    @Override
    public Map<String, Object> findLogin(String openid) {
        return userServiceManage.findLogin(openid);
    }


}
